(add-to-list 'load-path
	     (expand-file-name (concat user-emacs-directory "lisp")))

(setq custom-file (expand-file-name "custom.el" user-emacs-directory))

(require 'init-const)
(require 'init-unbindkey)
(require 'init-startup)
(require 'init-elpa)
(require 'init-ui)
(require 'init-packages)


;;--------------------------
;; 有些包在 在源里面下载不下来. 比如 which-key,直接手动下载下来
;; which-key
(add-to-list 'load-path
	     (expand-file-name 
	      (concat user-emacs-directory "extra_plugins/emacs-which-key")))
(require 'which-key)
(which-key-mode)


;;
(when (file-exists-p custom-file)
  (load-file custom-file))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 按照android studio的按键习惯
;; F4 格式化代码
(defun dai-format-code ()
  "格式化代码"
  (interactive)
  (if mark-active
      (indent-region (region-beginning) (region-end))
    (indent-region (point-min) (point-max)))
  )
(global-set-key [f4] 'dai-format-code)


;; C-d 删除行或者选中区域
(defun dai-kill-line ()
  "kill光标所在行"
  (interactive)
  (if mark-active
      (kill-region (region-beginning) (region-end))
    (progn
      (kill-region
       (line-beginning-position)
       (line-end-position))
      (kill-line)
      )    
    ))
(global-set-key (kbd "C-d") 'dai-kill-line)


;; 移动到行首
;; 为什么 os-x下无效
(global-set-key (kbd "<C-left>") 'beginning-of-line-text)

;; 移动到行尾
(defun dai-end-of-line-text ()
  "移动到行尾"
  (interactive)
  (end-of-line)
  (skip-chars-backward " \t")
  )

;; 为什么 os-x下无效
(global-set-key (kbd "C-<right>") 'dai-end-of-line-text)



;;--------------------------------
;; 一些不绑定的函数
(defun dai-select-inside-quotes ()
  "选择引号里面的内容"
  (interactive)
  (let (p1 p2)
    (skip-chars-backward "^\"")
    (setq p1 (point))
    (skip-chars-forward "^\"")
    (setq p2 (point))
    (goto-char p1)
    (push-mark p2)
    (setq mark-active t)))



(defun dai-get-word ()
  "打印光标所在位置的单词"
  (interactive)
  (let (p1
	p2
	(case-fold-search t))
    (save-excursion
      (skip-chars-backward "-_a-z0-9")
      (setq p1 (point))
      (skip-chars-forward "-_a-z0-9")
      (setq p2 (point))
      (message "%s" (buffer-substring-no-properties p1 p2)))))

(defun dai-get-line (n)
  "获取当前行,包含前后空格"
  (save-excursion
    (goto-line n)
    (buffer-substring-no-properties (line-beginning-position) (line-end-position))
    ))



;; 绑定 C-x C-c 为复制
;; 绑定 C-x C-v 为粘贴
(global-set-key (kbd "C-x C-c") 'kill-ring-save)
(global-set-key (kbd "C-x C-v") 'yank)
(global-set-key (kbd "C-x C-x") 'kill-region)

;; 函数跳转
(global-set-key (kbd "C-j") 'find-function-at-point)

;; 退出emacs
(global-set-key (kbd "C-x C-k k") 'save-buffers-kill-emacs)

(defun dai-insert-code-ref()
  "插入#+BEGIN_SRC #+END_SRC"
  (interactive)
  (when (eq major-mode 'org-mode)
    (beginning-of-line)
    (insert "#+BEGIN_SRC")
    (open-line 1)
    (next-line)
    (beginning-of-line)
    (insert "#+END_SRC")))

;; 可以用来记录光标位置
;; (defun remember-init ()
;;   "Remember current position and setup."
;;   (interactive)
;;   (point-to-register 8)
;;   (message "Have remember one position"))

;; (defun remember-jump ()
;;   "Jump to latest position and setup."
;;   (interactive)
;;   (let ((tmp (point-marker)))
;;     (jump-to-register 8)
;;     (set-register 8 tmp))
;;   (message "Have back to remember position"))



